package com.lib.common.shapeview.drawable;

import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

/**
 * TiyendeCredit
 *
 * @Author wwdeng
 * @CreateTime: 2024/12/4 19:57
 * @Annotation：
 */
public class ShapeState extends Drawable.ConstantState{
    public int changingConfigurations;
    public int shapeType = 0;
    public int solidGradientType = 0;
    public ShapeGradientOrientation solidGradientOrientation;
    public int[] solidColors;
    public int[] strokeColors;
    public int[] tempSolidColors;
    public float[] tempSolidPositions;
    public float[] positions;
    public boolean hasSolidColor;
    public boolean hasStrokeColor;
    public int solidColor;
    public int strokeSize;
    public ShapeGradientOrientation strokeGradientOrientation;
    public int strokeColor;
    public float strokeDashSize;
    public float strokeDashGap;
    public float radius;
    public float[] radiusArray;
    public Rect padding;
    public int width;
    public int height;
    public float ringInnerRadiusRatio;
    public float ringThicknessRatio;
    public int ringInnerRadiusSize;
    public int ringThicknessSize;
    public float solidCenterX;
    public float solidCenterY;
    public float gradientRadius;
    public boolean useLevel;
    public boolean useLevelForShape;
    public boolean opaque;
    public int shadowSize;
    public int shadowColor;
    public int shadowOffsetX;
    public int shadowOffsetY;
    public int lineGravity;

    public ShapeState() {
        this.solidGradientOrientation = ShapeGradientOrientation.TOP_TO_BOTTOM;
        this.strokeSize = -1;
        this.strokeGradientOrientation = ShapeGradientOrientation.TOP_TO_BOTTOM;
        this.width = -1;
        this.height = -1;
        this.ringInnerRadiusSize = -1;
        this.ringThicknessSize = -1;
        this.solidCenterX = 0.5F;
        this.solidCenterY = 0.5F;
        this.gradientRadius = 0.5F;
        this.lineGravity = 17;
    }

    public ShapeState(ShapeState state) {
        this.solidGradientOrientation = ShapeGradientOrientation.TOP_TO_BOTTOM;
        this.strokeSize = -1;
        this.strokeGradientOrientation = ShapeGradientOrientation.TOP_TO_BOTTOM;
        this.width = -1;
        this.height = -1;
        this.ringInnerRadiusSize = -1;
        this.ringThicknessSize = -1;
        this.solidCenterX = 0.5F;
        this.solidCenterY = 0.5F;
        this.gradientRadius = 0.5F;
        this.lineGravity = 17;
        this.changingConfigurations = state.changingConfigurations;
        this.shapeType = state.shapeType;
        this.solidGradientType = state.solidGradientType;
        this.solidGradientOrientation = state.solidGradientOrientation;
        if (state.solidColors != null) {
            this.solidColors = (int[])state.solidColors.clone();
        }

        if (state.strokeColors != null) {
            this.strokeColors = (int[])state.strokeColors.clone();
        }

        if (state.positions != null) {
            this.positions = (float[])state.positions.clone();
        }

        this.hasSolidColor = state.hasSolidColor;
        this.hasStrokeColor = state.hasStrokeColor;
        this.solidColor = state.solidColor;
        this.strokeSize = state.strokeSize;
        this.strokeColor = state.strokeColor;
        this.strokeDashSize = state.strokeDashSize;
        this.strokeDashGap = state.strokeDashGap;
        this.radius = state.radius;
        if (state.radiusArray != null) {
            this.radiusArray = (float[])state.radiusArray.clone();
        }

        if (state.padding != null) {
            this.padding = new Rect(state.padding);
        }

        this.width = state.width;
        this.height = state.height;
        this.ringInnerRadiusRatio = state.ringInnerRadiusRatio;
        this.ringThicknessRatio = state.ringThicknessRatio;
        this.ringInnerRadiusSize = state.ringInnerRadiusSize;
        this.ringThicknessSize = state.ringThicknessSize;
        this.solidCenterX = state.solidCenterX;
        this.solidCenterY = state.solidCenterY;
        this.gradientRadius = state.gradientRadius;
        this.useLevel = state.useLevel;
        this.useLevelForShape = state.useLevelForShape;
        this.opaque = state.opaque;
        this.shadowSize = state.shadowSize;
        this.shadowColor = state.shadowColor;
        this.shadowOffsetX = state.shadowOffsetX;
        this.shadowOffsetY = state.shadowOffsetY;
        this.lineGravity = state.lineGravity;
    }

    public Drawable newDrawable() {
        return new ShapeDrawable(this);
    }

    public Drawable newDrawable(Resources res) {
        return new ShapeDrawable(this);
    }

    public int getChangingConfigurations() {
        return this.changingConfigurations;
    }

    public void setType(int shape) {
        this.shapeType = shape;
        this.computeOpacity();
    }

    public void setSolidGradientType(int gradientType) {
        this.solidGradientType = gradientType;
    }

    public void setSolidColor(int... colors) {
        if (colors == null) {
            this.solidColor = 0;
            this.hasSolidColor = true;
            this.computeOpacity();
        } else {
            if (colors.length == 1) {
                this.hasSolidColor = true;
                this.solidColor = colors[0];
                this.solidColors = null;
            } else {
                this.hasSolidColor = false;
                this.solidColor = 0;
                this.solidColors = colors;
            }

            this.computeOpacity();
        }
    }

    public void setSolidColor(int argb) {
        this.hasSolidColor = true;
        this.solidColor = argb;
        this.solidColors = null;
        this.computeOpacity();
    }

    private void computeOpacity() {
        if (this.shapeType != 0) {
            this.opaque = false;
        } else if (!(this.radius > 0.0F) && this.radiusArray == null) {
            if (this.shadowSize > 0) {
                this.opaque = false;
            } else if (this.strokeSize > 0 && !isOpaque(this.strokeColor)) {
                this.opaque = false;
            } else if (this.hasSolidColor) {
                this.opaque = isOpaque(this.solidColor);
            } else {
                int[] var1;
                int var2;
                int var3;
                int color;
                if (this.solidColors != null) {
                    var1 = this.solidColors;
                    var2 = var1.length;

                    for(var3 = 0; var3 < var2; ++var3) {
                        color = var1[var3];
                        if (!isOpaque(color)) {
                            this.opaque = false;
                            return;
                        }
                    }
                }

                if (this.hasStrokeColor) {
                    this.opaque = isOpaque(this.strokeColor);
                } else {
                    if (this.strokeColors != null) {
                        var1 = this.strokeColors;
                        var2 = var1.length;

                        for(var3 = 0; var3 < var2; ++var3) {
                            color = var1[var3];
                            if (!isOpaque(color)) {
                                this.opaque = false;
                                return;
                            }
                        }
                    }

                    this.opaque = true;
                }
            }
        } else {
            this.opaque = false;
        }
    }

    private static boolean isOpaque(int color) {
        return (color >> 24 & 255) == 255;
    }

    public void setStrokeSize(int size) {
        this.strokeSize = size;
        this.computeOpacity();
    }

    public void setStrokeColor(int... colors) {
        if (colors == null) {
            this.strokeColor = 0;
            this.hasStrokeColor = true;
            this.computeOpacity();
        } else {
            if (colors.length == 1) {
                this.hasStrokeColor = true;
                this.strokeColor = colors[0];
                this.strokeColors = null;
            } else {
                this.hasStrokeColor = false;
                this.strokeColor = 0;
                this.strokeColors = colors;
            }

            this.computeOpacity();
        }
    }

    public void setCornerRadius(float radius) {
        if (radius < 0.0F) {
            radius = 0.0F;
        }

        this.radius = radius;
        this.radiusArray = null;
    }

    public void setCornerRadii(float[] radii) {
        this.radiusArray = radii;
        if (radii == null) {
            this.radius = 0.0F;
        }

    }
}
